package com.lc.advertising.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.request.CardListApi;
import com.lc.advertising.http.request.TixianAddApi;
import com.lc.advertising.http.response.CardListBean;
import com.lc.advertising.http.response.RegisterBean;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.util.CourseUtils;
import com.lc.advertising.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawalActivity extends AppActivity {
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private BaseQuickAdapter walladapter;
    private List<CardListBean.BankBean> dataBeanList=new ArrayList<>();
    private String bankId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawal;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        walladapter = new BaseQuickAdapter<CardListBean.BankBean, BaseViewHolder>(R.layout.item_withdrawal_bank) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, CardListBean.BankBean dataBean) {
                holder.setText(R.id.tv_bank,dataBean.getCardname());
                if (dataBean.isGrop()){
                    holder.setImageResource(R.id.iv_iv,R.mipmap.icon_shop_pay_select);
                }else {
                    holder.setImageResource(R.id.iv_iv,R.mipmap.icon_shop_pay_unselect);
                }



            }
        };
        walladapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                for (CardListBean.BankBean bean :dataBeanList){
                    bean.setGrop(false);
                }
                CardListBean.BankBean bean =dataBeanList.get(position);
                bean.setGrop(!bean.isGrop());
                if (bean.isGrop()){
                    bankId = dataBeanList.get(position).getId()+"";

                }
                walladapter.notifyDataSetChanged();

            }
        });
        recyclerview.setAdapter(walladapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        BankCart();

    }
    private void BankCart() {
        EasyHttp.post(this)
                .api(new CardListApi().setUid(SPUtil.get(this, IntentKey.UID,"")))
                .request(new HttpCallback<CardListBean>(this) {



                    @Override
                    public void onSucceed(CardListBean data) {

                        if (data.getCode()==200){
                            dataBeanList.clear();
                            dataBeanList.addAll(data.getBank());
                            if (dataBeanList.size()>0){
                                dataBeanList.get(0).setGrop(true);
                                bankId=dataBeanList.get(0).getId()+"";
                            }
                            walladapter.setNewData(dataBeanList);

                        }


                    }

                });

    }
    @OnClick({R.id.image_back, R.id.btn_recharge,R.id.rl_addbank})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.btn_recharge:
                tixian_add();
                break;
            case R.id.rl_addbank:
                startActivity(new Intent(WithdrawalActivity.this,AddBankCartActivity.class).putExtra("type","添加"));
                break;

        }
    }

    private void tixian_add() {
        if (CourseUtils.isNullOrEmpty(etMoney.getText().toString().trim())){
            toast("请输入提现金额");
            return;
        }
        EasyHttp.post(this)
                .api(new TixianAddApi()
                        .setUid(SPUtil.get(this,IntentKey.UID,"")).setBankid(bankId).setMoney(etMoney.getText().toString().trim()))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMessage());
                        if (data.getCode() == 200) {
                            finish();
                        }

                    }


                });
    }
}
